//
//  TTLuoshiViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/10/8.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTLuoshiViewCell.h"
#import "TTUpDownButton.h"
#import "TTOrderShopModel.h"
#import <UIImageView+WebCache.h>
#import "TTCommondDataModel.h"
#import "YBImageBrowser.h"

@interface TTLuoshiViewCell ()

@property (nonatomic, strong) TTOrderShopModel *model;

@property (nonatomic, strong) UIImageView *leftImageView;
///售罄图片
@property (nonatomic, strong) UIImageView *selloutImageView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *oneLabel;
@property (nonatomic, strong) UILabel *twoLabel;
@property (nonatomic, strong) UILabel *threeLabel;
@property (nonatomic, strong) UIButton *fourBtn;
@property (nonatomic, strong) TTUpDownButton *locationBtn;
@property (nonatomic, strong) UILabel *priceLabel;  // 价格
@property (nonatomic, strong) UIButton *exchangeLuoshiBtn;  //更换裸石

@property (nonatomic, assign) BOOL isFromDianpu;

@end

@implementation TTLuoshiViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupSubviews];
        
    }
    return self;
}

//更换裸石按钮
- (void)exchangeLuoshiBtnClick
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"exchangeLuoshiNotify_key" object:self.model];
}

- (void)setModel:(TTOrderShopModel *)model hasShadow:(BOOL)hasShadow isGray:(BOOL)isGray
{
    [self setModel:model hasShadow:hasShadow isGray:isGray isFromDianpu:NO];
}

- (void)setModel:(TTOrderShopModel *)model hasShadow:(BOOL)hasShadow isGray:(BOOL)isGray isFromOrderDetail:(BOOL)isFromOrderDetail orderStatus:(int)orderStatus
{
    [self setModel:model hasShadow:hasShadow isGray:isGray isFromDianpu:NO];
        //订单详情中裸石的处理(添加更换裸石按钮)
    if (isFromOrderDetail && orderStatus <= 60) {
        [_priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).mas_offset(-40);
            make.right.equalTo(self.contentView).mas_offset(-10);
        }];
        _exchangeLuoshiBtn.hidden = NO;
    } else {
        _exchangeLuoshiBtn.hidden = YES;
    }
}

- (void)setModel:(TTOrderShopModel *)model hasShadow:(BOOL)hasShadow isGray:(BOOL)isGray isFromDianpu:(BOOL)isFromDianpu
{
    _model = model;
    NSString *imageUrlString = [model.GoodsImage urlEncode];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:imageUrlString] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    self.leftLabel.text = model.ProductText;
    
    self.oneLabel.text = model.GoodsTitle;
    NSString *goodsDes = isFromDianpu ? model.GoodsDescribeTaotuo : model.GoodsDescribe;
    NSData *data = [goodsDes dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *describe = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    self.twoLabel.text = [describe componentsJoinedByString:@"\n"];
    
    NSString *value = @"";
    if (model.ClarityCharacteristics.length > 1) {
        NSDictionary *dic = [TTCommondDataModel getClarityCharacteristics];
        NSArray *characteristicsArray = [model.ClarityCharacteristics componentsSeparatedByString:@","];
        for (NSString *str in characteristicsArray) {
            NSString *key = [str removeFirstAndLastBlank];
            if (dic[key]) {
                value = [NSString stringWithFormat:@"%@%@ ", value, dic[key]];
            }
        }
    }
    if (value.length == 0) {
        self.threeLabel.text = model.ClarityCharacteristics;
    } else {
        self.threeLabel.text = value;
    }
    
    if (model.SouthAfrica) {
        [self.fourBtn setTitle:model.SouthAfricaText forState:UIControlStateNormal];
        self.fourBtn.enabled = NO;
    } else {
        [self.fourBtn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
        if (model.GoodsCertPic.length > 0) {
            NSString *zsNum = [NSString stringWithFormat:@"%@ %@", model.GoodsCertType, model.GoodsCertNo];
            NSString *zsStr = [NSString stringWithFormat:@"%@ 证书预览", zsNum];
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:zsStr];
            [attr addAttributes:@{NSUnderlineStyleAttributeName : @1, NSUnderlineColorAttributeName : kMainColor, NSForegroundColorAttributeName : kMainColor} range:[zsStr rangeOfString:@"证书预览"]];
            [attr addAttribute:NSForegroundColorAttributeName value:UIColorHex(333333) range:[zsStr rangeOfString:zsNum]];
            
            [self.fourBtn setAttributedTitle:attr forState:UIControlStateNormal];
            self.fourBtn.enabled = YES;
        } else {
            NSString *zsStr = [NSString stringWithFormat:@"%@ %@", model.GoodsCertType, model.GoodsCertNo];
            [self.fourBtn setTitle:zsStr forState:UIControlStateNormal];
            self.fourBtn.enabled = NO;
        }
    }
    
    [self.locationBtn setTitle:model.GoodsLocation forState:UIControlStateNormal];
    self.locationBtn.hidden = model.GoodsLocation.length == 0;
    
    NSString *price = isFromDianpu ? model.PlusShopGoodsPrice : model.GoodsPrice;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@", price];
    
    // 阴影
    [self setShadowWithHasShadow:hasShadow isGray:isGray];
    
    self.selloutImageView.hidden = model.SysStatus.intValue != 9;
}


- (void)setShadowWithHasShadow:(BOOL)hasShadow isGray:(BOOL)isGray
{
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH - 20, self.model.h - 10);
    self.contentView.layer.cornerRadius = 10;
    if (hasShadow) {
        // 阴影
        self.contentView.backgroundColor = UIColorHex(#F5F5F5); //[UIColor whiteColor];
//        self.contentView.layer.shadowColor = kMainColor.CGColor;
//        self.contentView.layer.shadowOffset = CGSizeZero;
//        self.contentView.layer.shadowOpacity = 0.2;
//        self.contentView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5].CGPath;
    } else {
           if (isGray) {
               // 灰色背景
               self.contentView.backgroundColor = UIColorHex(FBFBFB);
               
               UIBezierPath *bPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5];
               CAShapeLayer *layer = [CAShapeLayer layer];
               layer.strokeColor = UIColorHex(DEDEDE).CGColor;
               layer.lineWidth = 1;
               layer.lineCap = kCALineCapRound;
               layer.lineDashPattern = @[@5, @5];
               layer.path = bPath.CGPath;
               layer.fillColor = [UIColor clearColor].CGColor;
               [self.contentView.layer insertSublayer:layer atIndex:0];
               
           } else {
               self.contentView.backgroundColor = [UIColor clearColor];
           }
       }
}

// 查看证书
- (void)lookZSBtnClick
{
    NSString *urlString = self.model.GoodsCertPic;
    [TTLoginSingle lookCertificateWithUrlString:urlString];
}

- (void)setupSubviews
{
    _leftImageView = [[UIImageView alloc] init];
    _leftImageView.layer.cornerRadius = 10;
    _leftImageView.layer.masksToBounds = YES;
    _leftLabel.backgroundColor = UIColor.whiteColor;
    _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_leftImageView];
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(self.contentView).mas_offset(-12);
        make.width.height.mas_equalTo(65 * kScale);
    }];
    
    _selloutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopcart_sellout"]];
    _selloutImageView.hidden = YES;
    _selloutImageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_selloutImageView];
    [_selloutImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.leftImageView);
    }];
    
    _leftLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:9];
    [self.contentView addSubview:_leftLabel];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftImageView.mas_bottom).mas_offset(5);
        make.centerX.equalTo(self.leftImageView);
    }];
    
    _oneLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] fontSize:15];
    _twoLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    _threeLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:8];
    _fourBtn = [UIButton createButtonWithTitle:@"证书预览" titleColor:UIColorHex(333333) titleFont:[UIFont systemFontOfSize:12] backgroundColor:nil target:self action:@selector(lookZSBtnClick)];
    
    UIStackView *stView = [[UIStackView alloc] init];
    stView.axis = UILayoutConstraintAxisVertical;
    stView.distribution = UIStackViewDistributionFill;
    stView.alignment = UIStackViewAlignmentLeading;
    stView.spacing = 5;
    [self.contentView addSubview:stView];
    [stView addArrangedSubview:_oneLabel];
    [stView addArrangedSubview:_twoLabel];
    [stView addArrangedSubview:_threeLabel];
    [stView addArrangedSubview:_fourBtn];
    [stView addArrangedSubview:[UIView new]];
    [stView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.equalTo(self.leftImageView.mas_right).mas_offset(10);
        make.bottom.equalTo(self.contentView);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
     _locationBtn = [TTUpDownButton buttonWithType:UIButtonTypeCustom];
        [_locationBtn setTitleColor:UIColorHex(DEDEDE) forState:UIControlStateNormal];
        [_locationBtn setImage:[UIImage imageNamed:@"shopcart_dizhi"] forState:UIControlStateNormal];
    _locationBtn.titleLabel.font = [UIFont systemFontOfSize:9];
//    _locationBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.contentView addSubview:_locationBtn];
        [_locationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(stView);
            make.right.equalTo(self.contentView);
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(30);
        }];
    
    // 价格
    _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(FF1F1F) fontSize:15];
    [self.contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).mas_offset(-10);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    _exchangeLuoshiBtn = [UIButton createButtonWithTitle:@"更换裸石" titleColor:kMainColor titleFontSize:10 borderColor:kMainColor cornerRadius:10 contentEdgeInsets:UIEdgeInsetsZero target:self action:@selector(exchangeLuoshiBtnClick)];
    _exchangeLuoshiBtn.hidden = YES;
    [self.contentView addSubview:_exchangeLuoshiBtn];
    [_exchangeLuoshiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).mas_offset(-10);
        make.right.equalTo(self.contentView).mas_offset(-5);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(60);
    }];
    
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.x += 5;
    frame.size.width -= 10;

    frame.origin.y += 5;
    frame.size.height -= 10;

    [super setFrame:frame];
}

@end
